﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DOAN.DO;
using DOAN.BL;

namespace DOAN.Admin
{
    public partial class QL_Dethi : System.Web.UI.Page
    {
        string cauhoi = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["quyen"] == null)
                Response.Redirect("LoginAdmin.aspx");
            else
            {
                if ((Session["quyen"].ToString() == "AD") || (Session["quyen"].ToString() == "GV"))
                {
                    if (Session["Tendangnhap"] != null && Session["Tendangnhap"].ToString() != "")
                    {
                        MasterPage mt = this.Master as MasterPage;
                        LinkButton lb = mt.FindControl("LinkButton1") as LinkButton;
                        lb.Visible = false;
                        Label bl = mt.FindControl("lblDnthanhcong") as Label;
                        bl.Visible = true;
                        Label blten = mt.FindControl("lbtendn") as Label;
                        blten.Visible = true;
                        blten.Text = Session["Tendangnhap"].ToString();
                        LinkButton lbDangky = mt.FindControl("lbDangky") as LinkButton;
                        lbDangky.Visible = false;
                        LinkButton lbthoat = mt.FindControl("lbThoat") as LinkButton;
                        lbthoat.Visible = true;
                        Session["MaTK"].ToString();
                    }

                    if (!IsPostBack)
                    {
                        string tentaikhoan = Session["Tendangnhap"].ToString();
                        loadMon();
                    }
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
        }
        public void loadMon()
        {
            DataTable dt = new DataTable();
            if (Session["quyen"].ToString() == "GV")
                dt = new CT_MonhocBL().GetByUser(Session["Tendangnhap"].ToString());
            else if (Session["quyen"].ToString() == "AD")
                dt = new MonhocBL().SelectAll();
            if (dt.Rows.Count > 0)
            {
                drpMon.DataSource = dt;
                drpMon.DataTextField = "Tenmon";
                drpMon.DataValueField = "Mamonhoc";
                drpMon.DataBind();
            }
            else
            {
                drpMon.Text = "Khong co mon";
            }
        }

        public void load()
        {

            int i = int.Parse((string)(new MonhocBL().GetSoChuong(drpMon.SelectedValue)));

            for (int j = 1; j <= i; j++)
            {
                //Label lbl = (Label)rpt.FindControl("lblChuong");
                //lbl.Text = "So cau chuong " + j;
            }
        }

        protected void btTao_Click(object sender, EventArgs e)
        {
            if (int.Parse(txtTong.Text) != (int.Parse(txtDe.Text) + int.Parse(txtKho.Text) + int.Parse(txtTB.Text)))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Không đủ số câu tạo đề!');", true);
            }
            else
            {
                GridDe.Visible = true;
                DataTable dt = new CauhoiBL().TaoDe(drpMon.SelectedValue, int.Parse(txtKho.Text), int.Parse(txtTB.Text), int.Parse(txtDe.Text));
                int count = GridDethi.Rows.Count;

                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (j == 0)
                        //ar[j] = dethi.Rows[j].ItemArray[0].ToString();
                        cauhoi = dt.Rows[j].ItemArray[0].ToString();
                    else
                        //ar[j] = "-" + dethi.Rows[j].ItemArray[0].ToString();
                        cauhoi += "-" + dt.Rows[j].ItemArray[0].ToString();
                }
                Session["DSCH"] = cauhoi;
                GridDethi.Columns[0].Visible = false;
                GridDethi.DataSource = dt;
                GridDethi.DataBind();
                btLuu.Visible = true;
                Button2.Visible = false;
            }
        }

        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                int i = int.Parse((string)(new MonhocBL().GetSoChuong(drpMon.SelectedValue)));

                for (int j = 1; j <= i; j++)
                {
                    ((Label)e.Item.FindControl("lblChuong")).Text = "So cau chuong " + j;
                }
            }
        }

        protected void btLuu_Click(object sender, EventArgs e)
        {
            //foreach (DataRow dr in GridDethi.Rows)
            //{
            //    if (cauhoi.Count == 0)
            //        cauhoi.Add(dr[0].ToString());
            //    else
            //    {
            //        string s = "-" + dr[0].ToString();
            //        cauhoi.Add(s);
            //    }
            //}



            //for (int i = 0; i < count; i++)
            //{
            //    GridViewRow row = GridDethi.Rows[i];
            //    if (i == 0)
            //    {
            //        ar[i] = ((DataBoundLiteralControl)row.Cells[0].Controls[0]).Text;
            //        cauhoi.Add(row.Cells[0].ToString());
            //    }
            //    else
            //    {
            //        string s = "-" + ((DataBoundLiteralControl)row.Cells[0].Controls[0]).Text;
            //        ar[i] = s;
            //        cauhoi.Add(s);
            //    }
            //}
            DethiDO obj = new DethiDO();
            obj.Mamonhoc = drpMon.SelectedValue;
            obj.Ngaytao = DateTime.Now.ToShortDateString();
            obj.Socau = int.Parse(txtTong.Text);
            obj.Sodiem = 100;
            obj.Tentaikhoan = Session["Tendangnhap"].ToString();
            obj.Thoigian = 15;
            obj.Tieude = "TieuDe";
            //obj.Trangthai =bool.Parse(drTT.SelectedValue.ToString());
            obj.Trangthai = true;
            obj.Macauhoi = Session["DSCH"].ToString();
            new DethiBL().Insert(obj);
        }

        protected void rdTD_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTD.Checked == true)
            {
                btLuu.Visible = true;
                pnTaoDeTD.Visible = true;
                GridDe.Visible = false;
            }
            else
            {
                pnTaoDeTD.Visible = false;
                GridDe.Visible = true;
                btLuu.Visible = false;
                GridDethi.Columns[0].Visible = true;
                DataTable dt = new DataTable();
                dt = new CauhoiBL().GetByMon(drpMon.SelectedValue);
                if (dt.Rows.Count > 0)
                {
                    GridDethi.DataSource = dt;
                    GridDethi.DataBind();
                }
                else
                {
                    GridDethi.DataBind();
                    GridDethi.Attributes.Add("EmptyDataText", "Khong co du lieu");
                }
            }

        }

        protected void rdTC_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTC.Checked == false)
            {
                pnTaoDeTD.Visible = true;
                GridDe.Visible = false;
            }
            else
            {
                pnTaoDeTD.Visible = false;
                GridDe.Visible = true;
                btLuu.Visible = false;
                Button2.Visible = true;
                GridDethi.Columns[0].Visible = true;
                DataTable dt = new DataTable();
                dt = new CauhoiBL().GetByMon(drpMon.SelectedValue);
                if (dt.Rows.Count > 0)
                {
                    GridDethi.DataSource = dt;
                    GridDethi.DataBind();
                }
                else
                {
                    GridDethi.DataBind();
                    GridDethi.Attributes.Add("EmptyDataText", "Khong co du lieu");
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //foreach (GridViewRow row in GridDethi.Rows)
            //{
            //    CheckBox check = (CheckBox)row.FindControl("ckb");
            //    if (check != null && check.Checked)
            //    {
            //        if (row.RowIndex == 0)
            //            cauhoi = row;
            //        else
            //            //ar[j] = "-" + dethi.Rows[j].ItemArray[0].ToString();
            //            cauhoi += "-" + row.Cells[0].Text;
            //    }
            //}
            int tong = 0;
            for (int i = 0; i < GridDethi.Rows.Count; i++)
            {
                CheckBox check = (CheckBox)GridDethi.Rows[i].FindControl("ckb");
                if (check != null && check.Checked)
                {
                    cauhoi += ((Label)GridDethi.Rows[i].FindControl("lblMaCH")).Text + "-";
                    tong++;
                }
            }
            cauhoi.Substring(0, cauhoi.Length - 2);
            DethiDO obj = new DethiDO();
            obj.Mamonhoc = drpMon.SelectedValue;
            obj.Ngaytao = DateTime.Now.ToShortDateString();
            obj.Socau = tong;
            obj.Sodiem = 100;
            obj.Tentaikhoan = Session["Tendangnhap"].ToString();
            obj.Thoigian = 15;
            obj.Tieude = "TieuDe";
            obj.Trangthai = true;
            obj.Macauhoi = cauhoi;
            new DethiBL().Insert(obj);

        }

        protected void GridDethi_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //DataRowView obj = e.Row.DataItem;
            //CheckBox cbCheck = e.Row.FindControl("ckb");
            //if (cbCheck.Checked == true)
            //{
            //}
        }
    }
}
